{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "05597dd0-96c7-4860-b2a9-2db373efed8e",
   "metadata": {},
   "source": [
    "<font size=5>**Psutil Module**用法</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb4e5ca7-51c0-49e3-acce-e9f270bec775",
   "metadata": {},
   "source": [
    "|序号|模块|函数|功能|\n",
    "|---|---|---|---|\n",
    "|1|CPU|psutil.cpu_times|计算总/各核心累计消耗的时间（秒）|\n",
    "|2|CPU|psutil.cpu_times_percent|计算总/各核心累计消耗的时间（%）|\n",
    "|3|CPU|psutil.cpu_stats() ||\n",
    "|4|CPU|psutil.cpu_count() ||\n",
    "|5|CPU|psutil.cpu_freq()  ||\n",
    "|6|Memory|psutil.virtual_memory() ||\n",
    "|7|Memory|psutil.swap_memory() ||\n",
    "|8|Disk|psutil.disk_partitions() ||\n",
    "|9|Disk|psutil.disk_usage('/') ||\n",
    "|10|Disk|psutil.disk_io_counters(perdisk=True) ||\n",
    "|11|Network|psutil.net_io_counters(pernic=True)||\n",
    "|12|Network|psutil.net_if_addrs()    ||\n",
    "|13|Network|psutil.net_if_stats()  ||\n",
    "|14|Senors|psutil.sensors_temperatures()  ||\n",
    "|15|Senors|psutil.sensors_fans()   ||\n",
    "|16|Senors|psutil.sensors_battery()  ||\n",
    "|17|Process|psutil.pids()  ||\n",
    "|18|Process|psutil.Process() ||\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T12:07:18.936200Z",
     "start_time": "2025-07-26T12:07:18.886214Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import datetime\n",
    "import psutil as pu\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84ee5b42-6d0c-4f9c-8a45-5701e2a2661c",
   "metadata": {},
   "source": [
    "## 1.psutil.cpu_times()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c5fcfe5b-7dc7-47f7-9d48-3813ea90d845",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户进程占用的CPU时间（h）： 0.07491753472222222\n",
      "系统内核占用的CPU时间（h）： 0.07484375\n",
      "CPU的空闲时间（h）： 1.7007204861111112\n",
      "CPU处理硬件中断的时间（h）： 0.005842013888888889\n",
      "CPU处理延迟过程调用的时间（h）： 0.019787326388888887\n"
     ]
    }
   ],
   "source": [
    "# “把操作系统自开机以来，CPU 在各种状态下累计消耗的时间（以秒为单位的浮点数）一次性打包返回。”\n",
    "cpu_times_all = pu.cpu_times(percpu=True)  # 返回每个核心的运行时间\n",
    "cpu_times = cpu_times_all[0]  # 以第一个核心为例\n",
    "\n",
    "print('用户进程占用的CPU时间（h）：', cpu_times.user / 3600)\n",
    "print('系统内核占用的CPU时间（h）：', cpu_times.system / 3600)\n",
    "print('CPU的空闲时间（h）：', cpu_times.idle / 3600)\n",
    "print('CPU处理硬件中断的时间（h）：', cpu_times.interrupt / 3600)\n",
    "print('CPU处理延迟过程调用的时间（h）：', cpu_times.dpc / 3600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "984aa938-1ab0-4831-8e7d-20d1a5fb2487",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU自开机起运行的总时间： 1.88 h\n"
     ]
    }
   ],
   "source": [
    "# 计算开机到现在的总计时间（近似）\n",
    "print('CPU自开机起运行的总时间：', \n",
    "      np.round((cpu_times.user + cpu_times.system + cpu_times.idle + \n",
    "                cpu_times.interrupt + cpu_times.dpc) / 3600, 2), 'h')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7d3a160-fb3f-4315-9033-ab48999481b3",
   "metadata": {},
   "source": [
    "## 2.psutil.boot_time()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6c4217a7-c2f6-427d-a532-6939d72dbcd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开机启动时间： 2025-07-26 18:17:19\n"
     ]
    }
   ],
   "source": [
    "# 计算开机到现在的总计时间（准确）\n",
    "boot_time = pu.boot_time()\n",
    "boot_time = datetime.datetime.fromtimestamp(boot_time)\n",
    "print(\"开机启动时间：\", boot_time.strftime('%Y-%m-%d %H:%M:%S'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d5d8dde-ca04-4148-9ea8-bbe413529030",
   "metadata": {},
   "source": [
    "## 3.psutil.cpu_times_percent()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8d24749b-8ad0-4060-95ab-1e0fa62f967d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户进程占用的CPU时间（%）： 3.1\n",
      "系统内核占用的CPU时间（%）： 0.0\n",
      "CPU的空闲时间（%）： 96.9\n",
      "CPU处理硬件中断的时间（%）： 0.0\n",
      "CPU处理延迟过程调用的时间（%）： 0.0\n"
     ]
    }
   ],
   "source": [
    "# “把操作系统自开机以来，CPU 在各种状态下累计消耗的时间（以%为单位的浮点数）一次性打包返回。”\n",
    "cpu_times_percent_all = pu.cpu_times_percent(interval=1, percpu=True)  # 阻塞1秒钟，同时返回每个核心的运行时间\n",
    "cpu_times_percent = cpu_times_percent_all[0]  # 以第一个核心为例\n",
    "\n",
    "print('用户进程占用的CPU时间（%）：', cpu_times_percent.user)\n",
    "print('系统内核占用的CPU时间（%）：', cpu_times_percent.system)\n",
    "print('CPU的空闲时间（%）：', cpu_times_percent.idle)\n",
    "print('CPU处理硬件中断的时间（%）：', cpu_times_percent.interrupt)\n",
    "print('CPU处理延迟过程调用的时间（%）：', cpu_times_percent.dpc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8b68c94b-8f90-4c98-a736-1f39c09d5a79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "scputimes(user=5.0, system=7.5, idle=85.5, interrupt=0.7, dpc=1.3)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cpu_times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abb55e4f-64d9-4327-874b-6ae70649043b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9768f45-0a85-4154-9c7d-111f27f4a649",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f710cd48-1c96-401d-95e2-780a5cb16b6e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6213d86d-d82a-47bc-a1b7-4dfd7117b473",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98828610-9d57-48d9-8613-b61bc1cb1320",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d620b717-7a15-427f-b6cc-a85b53471fce",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47265465-83cf-468d-bbf4-d457f7150ca6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
